/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package Admin.DAO;

import Admin.Model.postType;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author home
 */
public class PostTypeDAO {
    private Connection myCon;
    private PreparedStatement ps;
    private ResultSet rs;
    private String strCommand = "";

    public ArrayList<postType> GetAll() {
        ArrayList<postType> listPostType = new ArrayList<postType>();
        postType postType = null;
        try {

            strCommand = "select * from tbl_PostType";
            myCon = myConnection.getConnection();
            myConnection.getConnection();
            ps = myCon.prepareStatement(strCommand);

            rs = ps.executeQuery();
            while (rs.next()) {
                postType = new postType();

                postType.setPostTypeID(rs.getInt("postTypeID"));
                postType.setPostTypeName(rs.getString("postTypeName"));
                postType.setPostTypePrice(rs.getString("postTypePrice"));
                postType.setPostTypeStatus(rs.getInt("postTypeStatus"));

                listPostType.add(postType);
            }
            return listPostType;
        } catch (SQLException ex) {
            Logger.getLogger(PostTypeDAO.class.getName()).log(Level.SEVERE, null, ex);
        } finally {
            myConnection.closeConnection(myCon, ps, rs);
        }
        return listPostType;
    }

    public boolean AddPostType(String postTypeName, String postTypePrice) {
        boolean flag = false;
        try {

            strCommand = "INSERT INTO tbl_PostType (postTypeName,postTypePrice,postTypeStatus) VALUES (?,?,1)";
            myCon = myConnection.getConnection();
            ps = myCon.prepareStatement(strCommand);
            ps.setString(1, postTypeName);
            ps.setString(2, postTypePrice);
            int i = ps.executeUpdate();
            if (i > 0) {
                flag = true;
            }

        } catch (SQLException ex) {
            Logger.getLogger(PostTypeDAO.class.getName()).log(Level.SEVERE, null, ex);
        } finally {
            myConnection.closeConnection(myCon, ps, null);
        }
        return flag;
    }

    public postType GetByID(int postTypeID) {

        postType postType = new postType();
        try {

            strCommand = "select * from tbl_PostType WHERE postTypeID=?";

            myCon = myConnection.getConnection();
            ps = myCon.prepareStatement(strCommand);
            ps.setInt(1, postTypeID);
            rs = ps.executeQuery();
            while (rs.next()) {
                postType.setPostTypeID(rs.getInt("postTypeID"));
                postType.setPostTypeName(rs.getString("postTypeName"));
                postType.setPostTypePrice(rs.getString("postTypePrice"));
                postType.setPostTypeStatus(rs.getInt("postTypeStatus"));
            }
            return postType;
        } catch (SQLException ex) {
            Logger.getLogger(PostTypeDAO.class.getName()).log(Level.SEVERE, null, ex);
        } finally {
            myConnection.closeConnection(myCon, ps, rs);
        }
        return postType;
    }

    public boolean changeStatus(int postTypeID, int postTypeStatus) {
        boolean flag = false;
        try {
            strCommand = "UPDATE tbl_PostType SET postTypeStatus = ? WHERE postTypeID = ?";
            myCon = myConnection.getConnection();
            ps = myCon.prepareStatement(strCommand, ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE);
            ps.setInt(1, postTypeStatus);
            ps.setInt(2, postTypeID);
            int i = ps.executeUpdate();
            if (i > 0) {
                flag = true;
            }
        } catch (SQLException ex) {
            ex.printStackTrace();
        } finally {
            myConnection.closeConnection(myCon, ps, null);
        }
        return flag;
    }

    public boolean editPostType(postType objPostType) {
        //UPDATE tbl_Banks SET bankName = ? WHERE bankID = ?
        boolean flag = false;
        try {
            strCommand = "UPDATE tbl_PostType SET postTypeName = ?,postTypePrice = ?,postTypeStatus = ? WHERE  postTypeID = ? ";
            myCon = myConnection.getConnection();
            ps = myCon.prepareStatement(strCommand, ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE);
            ps.setString(1, objPostType.getPostTypeName());
            ps.setString(2, objPostType.getPostTypePrice());
            ps.setInt(3, objPostType.getPostTypeStatus());
            ps.setInt(4, objPostType.getPostTypeID());
            int i = ps.executeUpdate();
            if (i > 0) {
                flag = true;
            }
        } catch (SQLException ex) {
            ex.printStackTrace();
        } finally {
            myConnection.closeConnection(myCon, ps, null);
        }
        return flag;
    }
    public boolean deletePostType(int postTypeID)
    {
         boolean flag = false;
        try {
            strCommand = "DELETE FROM tbl_PostType WHERE postTypeID = ?";
            myCon = myConnection.getConnection();
            ps = myCon.prepareStatement(strCommand, ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE);
            ps.setInt(1, postTypeID);
            int i = ps.executeUpdate();
            if (i > 0) {
                flag = true;
            }
        } catch (SQLException ex) {
            ex.printStackTrace();
        } finally {
            myConnection.closeConnection(myCon, ps, null);
        }
        return flag;
    }
}
